home *** CD-ROM | disk | FTP | other *** search
- /*
- * riphelp.c
- * Extracts help data from Acorn's !SrcEdit and turns it into a help system
- * for !StrongHlp.
- *
- * Version: 13feb93
- * Copyright (C) Mark H. Wilkinson 1993.
- */
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- typedef enum { NO = 0, YES = 1} Boolean;
-
-
-
- /*
- * This is where the prepared help pages are stored; these bits of text are
- * not run through fix_text() and so may include link words. Extra pages
- * should be added in separate files and #included to keep this file as
- * short as possible. The title page is last in the list if only to ensure
- * that the last initialiser doesn't have a comma after it (thanks ANSI!).
- */
-
- typedef struct {
- char *name;
- Boolean searchable;
- char *text;
- } prepared_help_page;
-
- static prepared_help_page help[] = {
-
- #include "operators.c"
-
- "$", NO,
- "The C Programming Language\n"
- "Help on the C programming language\n"
- "\n"
- " <Operators>\n"
- "\n"
- "Text prepared by Mark H. Wilkinson"
-
- };
-
-
-
- /*
- * Code to create an in-memory list of help pages. The list is sorted
- * alphabetically by name.
- */
-
- typedef struct _index_entry {
- int name_offset; /* In the index file */
- int text_offset; /* In the help data file */
- int text_length;
- int flags;
- } index_entry;
-
- typedef struct _node {
- index_entry index;
- char *name;
- char *text;
- struct _node *next; /* Yes folks, it's a linked list... */
- } node;
-
- static int entries;
- static node *head;
- static FILE *HelpData, *Index;
-
- static int cstrcmp(char *s1, char *s2)
- {
- int c1, c2;
-
- do
- {
- c1 = *s1++;
- if (isupper(c1)) c1 = tolower(c1);
- c2 = *s2++;
- if (isupper(c2)) c2 = tolower(c2);
- }
- while (c1 && c1 == c2);
- return(c1 - c2);
- }
-
-
- static void new_entry(char *name, char *text, Boolean searchable)
- {
- node *new, **test;
-
- if ((new = malloc(sizeof(node))) != NULL &&
- (new->name = malloc(strlen(name)+1)) != NULL &&
- (new->text = malloc(strlen(text)+1)) != NULL)
- {
- new->index.text_length = strlen(text);
- new->index.flags = searchable ? 0 : 1;
- strcpy(new->name, name);
- strcpy(new->text, text);
- test = &head;
- while (*test != NULL && cstrcmp(new->name, (*test)->name) >= 0)
- test = &((*test)->next);
- new->next = *test;
- *test = new;
- ++entries;
- return;
- }
- fprintf(stderr, "Out of memory.\n");
- exit(EXIT_FAILURE);
- }
-
-
- static void build_help(void)
- {
- node *current;
- int name_offset;
-
- HelpData = fopen("<StrongHelp$Dir>.HelpData.C.HelpData", "wb");
- Index = fopen("<StrongHelp$Dir>.HelpData.C.Index", "wb");
-
- current = head;
- while (current != NULL)
- {
- current->index.text_offset = (int) ftell(HelpData);
- fwrite(current->text, sizeof(char), current->index.text_length, HelpData);
- current = current->next;
- }
-
- fwrite(&entries, sizeof(int), 1, Index);
-
- name_offset = sizeof(int) + entries * sizeof(index_entry);
- current = head;
- while (current != NULL)
- {
- current->index.name_offset = name_offset;
- fwrite(¤t->index, sizeof(index_entry), 1, Index);
- name_offset += strlen(current->name)+1;
- current = current->next;
- }
-
- current = head;
- while (current != NULL)
- {
- fwrite(current->name, sizeof(char), strlen(current->name)+1, Index);
- current = current->next;
- }
- }
-
-
- /*
- * Put backslashes into a piece of text so that !StrongHlp displays it as
- * normal text with no embedded buttons.
- */
- void fix_entry(char *s)
- {
- char *f, *t;
- int c = 0;
-
- for (f = s; *f != '\000'; ++f)
- if (*f == '<') ++c;
-
- for (t = f + c; c > 0; --f, --t) {
- *t = *f;
- if (*f == '<') {
- *--t = '\\';
- --c;
- }
- }
- }
-
-
- static char line[100], name[100], text[5000]; /* Upper limits on text size */
- static FILE *Source;
-
- int main()
- {
- char *eol;
- int i;
-
- Source = fopen("<SrcEdit$Dir>.help.C", "r");
-
- if (Source == NULL)
- {
- fputs("riphelp: Can't open <SrcEdit$Dir>.help.C -\n"
- " has !SrcEdit been loaded?\n", stderr);
- exit(EXIT_FAILURE);
- }
-
- for (i = 0; i < sizeof(help)/sizeof(help[0]); i++)
- new_entry(help[i].name, help[i].text, help[i].searchable);
-
- while (!feof(Source))
- {
- fgets(line, 100, Source);
- switch (line[0]) {
- case '%' :
- eol = text+strlen(text)-1;
- while (*eol == '\n')
- *eol-- = '\000';
- if (name[0] != '\000')
- {
- fix_entry(text);
- new_entry(name, text, YES);
- }
- eol = strchr(line+1, '\n');
- *eol = '\000';
- strcpy(name, line+1);
- *eol = '\n';
- strcpy(text, line+1);
- break;
- default :
- strcat(text, line);
- break;
- }
- }
-
- build_help();
- }
-